package weaver.ihg.action;

import cn.hutool.http.HttpUtil;
import com.weaver.general.Util;
import weaver.contractmanagement.commons.JsonUtils;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

public class WebTaskSingelServletAction extends BaseBean implements Action {
    private static final String ACTIONNAME = "WebTaskSingelServletAction";

    private String cjrFiledName;//配置创建人的表单字段名字

    public String getCjrFiledName() {
        return cjrFiledName;
    }

    public void setCjrFiledName(String cjrFiledName) {
        this.cjrFiledName = cjrFiledName;
    }


    @Override
    public String execute(RequestInfo requestInfo) {
        cjrFiledName="hdcjr";
        BaseBean baseBean=new BaseBean();
        baseBean.writeLog("1111===>"+ requestInfo);
        HttpServletRequest request = requestInfo.getRequestManager().getRequest();
        String requestURI = request.getRequestURI();
        String remoteHost = request.getRemoteHost();
        int port=request.getServerPort();
        String protocol=request.getProtocol();
        String prefix="";
        if(protocol.contains("HTTPS")){
            prefix="https";
        }else if(protocol.contains("HTTP")){
            prefix="http";
        }
        baseBean.writeLog("requestURI==>"+requestURI+"===port===>"+port+"===remoteHost====>"+remoteHost+"====protocol===>"+protocol+"====>prefix==>"+prefix);

        List<String> psList=new ArrayList<>();
        psList.add(cjrFiledName);
        //修改参数字段大小写111
        Map<String, Object> masterFiledStr = showFormProperty(psList, requestInfo, baseBean);
        String apiUrl=prefix+"://"+remoteHost+"/changeOpertorMethod"+"?requestid="+requestInfo.getRequestid()+"&nodeid="+requestInfo.getRequestManager().getNodeid()+"&workflowid="+requestInfo.getRequestManager().getWorkflowid()
                +"&field5="+masterFiledStr.get(cjrFiledName)+"&operate=save&forwardflag=3";
        baseBean.writeLog("apiUrl===>"+apiUrl);
        String apiResponseBody= HttpUtil.createGet(apiUrl).execute().body();
        baseBean.writeLog("apiResponseBody===>"+apiResponseBody);
        return null;
    }
    /**
     * 获取主表数据
     */
    private Map<String, Object> showFormProperty(List<String> needPrams, RequestInfo requestInfo, BaseBean baseBean) {
        baseBean.writeLog("needParams==>" + JsonUtils.stringify(needPrams));
        Map<String, Object> resultMap = new HashMap<>();
        baseBean.writeLog("获取主表数据 ...");
        // 获取表单主字段值
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        baseBean.writeLog("properties==>" + Arrays.toString(properties));
        for (Property property : properties) {
            // 主字段名称
            String name = property.getName();
            // 主字段对应的值
            String value = Util.null2String(property.getValue());
            baseBean.writeLog("name: " + name + "===>value: " + value);
            for (String needPram : needPrams) {
                if (name.equalsIgnoreCase(needPram)) {
                    resultMap.put(name, value);
                }
            }


        }
        return resultMap;
    }

}
